package servletUtenteReg;

import eccezioni.NoNotificheException;
import gestioneContesto.GestoreContesto;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.Notifica;

import logicaUtenteReg.GestoreNotificheRemote;

/**
 * Servlet implementation class NotificheServlet
 */
public class NotificheServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private RequestDispatcher dispatcher;
	private GestoreContesto contesto;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NotificheServlet() {

		super();
		contesto = new GestoreContesto();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			eseguiRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void eseguiRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException, ParseException {

		GestoreNotificheRemote gestore = contesto.getGestore(GestoreNotificheRemote.class, "GestoreNotificheRemote");

		String username = request.getParameter("username");
		String nome = request.getParameter("nome");
		String cognome = request.getParameter("cognome");

		try {

			ArrayList<Notifica> notifiche = gestore.notifiche(username);

			for(int i = 0; i < notifiche.size(); i++) {

				request.setAttribute("el" + i, notifiche.get(i).getMsg());
				request.setAttribute("tipo" + i, notifiche.get(i).getTipo());

			}

			request.setAttribute("size", notifiche.size());

		} 
		catch (NoNotificheException e) {

			request.setAttribute("size", 0);
			request.setAttribute("err", e.getMessaggio());

		}

		ArrayList<Notifica> notComm = gestore.notComm(username);

		if(notComm.size() == 0 || notComm == null) {
			
			request.setAttribute("sizeComm", 0);
			
		}
		else {

			for(int j = 0; j < notComm.size(); j++) {

				request.setAttribute("elcomm" + j, notComm.get(j).getMsg());
				request.setAttribute("tipocomm" + j, notComm.get(j).getTipo());

			}
			
			request.setAttribute("sizeComm", notComm.size());

		}

		request.setAttribute("username", username);
		request.setAttribute("nome", nome);
		request.setAttribute("cognome", cognome);

		dispatcher = request.getRequestDispatcher("Notifiche.jsp");
		dispatcher.forward(request, response);

	}

}
